package chapter6.meine.exercises;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import chapter5.v2.BlackjackHand;
import chapter5.v2.Card;
import chapter5.v2.Deck;
import chapter5.v2.Hand;

public class BlackJackGUIPanel extends JPanel{
	private JButton buttHit, buttStand, buttNewGame;
	private JPanel panelCenter, panelBottom;
	private JLabel dealerMess, playerMess, mainMess;
	private String message;
	private boolean isGameIsGoing = true;
	private JTextField betField;

	public BlackJackGUIPanel() {
		setLayout(new BorderLayout(3, 3));
		setBackground(new Color(10,180,40) );
		setBorder(BorderFactory.createLineBorder(Color.gray, 3));
		
		
		CardPanel cardPanel = new CardPanel();
		cardPanel.setLayout(new BorderLayout());
		cardPanel.setForeground(Color.cyan);
		cardPanel.setOpaque(false);
		add(cardPanel);
		
		panelBottom = new JPanel();
		add(panelBottom, BorderLayout.SOUTH);

		buttHit = new JButton("Hit!");
		buttHit.addActionListener(cardPanel);
		panelBottom.add(buttHit);

		buttStand = new JButton("Stand!");
		buttStand.addActionListener(cardPanel);
		panelBottom.add(buttStand);

		buttNewGame = new JButton("New Game!");
		buttNewGame.addActionListener(cardPanel);
		panelBottom.add(buttNewGame);
		
		JLabel betLabel = new JLabel("Enter your bet: ");
		betLabel.setAlignmentX(RIGHT_ALIGNMENT);
		panelBottom.add(betLabel);
		betField = new JTextField("", 5);
		betField.addActionListener(cardPanel);
		panelBottom.add(betField);

	} // end of BlackJackGUIPanel() constructor
	
	private class CardPanel extends JPanel implements ActionListener {

		private Card card;
		private Deck deck;
		private BlackjackHand userHand, dealerHand;
		private int xCor, yCor, cardWidth, cardHeight, cardCount, bank, bet;
		private boolean isGameIsGoing;

		CardPanel() {
			setPreferredSize(new Dimension(400, 150));
			startNewGame();
		}

		public void actionPerformed(ActionEvent e) {
			if (e.getActionCommand() == "Hit!") {
				doHit();
			} else if (e.getActionCommand() == "Stand!") {
				doStand();
			} else if (e.getActionCommand() == "New Game!") {
				startNewGame();
				try {
				bet = Integer.parseInt(betField.getText());
				}
				catch (NumberFormatException ex) {
		            // The string xStr is not a legal number.
		            message= "Enter Your bet >0! Only numbers allowed";
		            return;
		         }
			}

		}// end of actionPerformed()

		void startNewGame() {
			if (isGameIsGoing) {
				message = "Finish this game first!";
				repaint();
				return;
			}

			deck = new Deck();
			card = new Card();
			userHand = new BlackjackHand();
			dealerHand = new BlackjackHand();
			
			deck.shuffle();
			for (int i = 0; i<2;i++){
			card = deck.dealCard();
			userHand.addCard(card);
			}
			for (int i = 0; i<2;i++){
			card = deck.dealCard();
			dealerHand.addCard(card);
			}
			isGameIsGoing = true;
			
			if (dealerHand.getBlackjackValue()==21) {
				isGameIsGoing = false;
				message = "You've lost to Dealer's BlackJack! Start a new game";
			}
				
			
			repaint();
		}// end of startNewGame()

		void doHit() {
			if (isGameIsGoing == false) {
				message = "Click button \"New Game\" to start a new game";
				repaint();
				return;
			}

			Card thisCard = deck.dealCard();
			userHand.addCard(thisCard);
			cardCount = userHand.getCardCount();
			if (userHand.getBlackjackValue()>21){
				isGameIsGoing = false;
				message = "You've lost: total score is bigger than 21. Try next game!";
				return;
			}
	
			repaint();
		}

		void doStand() {
			if (isGameIsGoing == false) {
				message = "Click button \"New Game\" to start a new game";
				repaint();
				return;
			}
			
			while (dealerHand.getBlackjackValue() <=16){

			Card thisCard = deck.dealCard();
			dealerHand.addCard(thisCard);
			cardCount = dealerHand.getCardCount();
			}
			if (dealerHand.getBlackjackValue()>21){
				isGameIsGoing = false;
				message = "You WIN, dealer has overscore!";
				
			}
			else if (dealerHand.getBlackjackValue()==21){
				isGameIsGoing = false;
				message = "You've lost on Dealer's BlackJack!";
				
			}
			else if (userHand.getBlackjackValue()==21){
				isGameIsGoing = false;
				message = "You WIN with BlackJack!";
				
			}
			else if (userHand.getBlackjackValue()>dealerHand.getBlackjackValue()){
				isGameIsGoing = false;
				message = "You WIN!";
				
			}
			else {
				isGameIsGoing = false;
				message = "You've LOST!";				
			}
			
			repaint();
		}

		public void paintComponent(Graphics g) {

			xCor = 20;
			yCor = 40;
			cardWidth = 60;
			cardHeight = 100;
			cardCount = dealerHand.getCardCount();
			for (int i = 0; i < cardCount; i++) {
				drawCard(dealerHand.getCard(i), g, xCor + i * (20 + cardWidth),
						yCor);
			}
			if (isGameIsGoing)
				drawCard(null, g, xCor, yCor);
			
			g.setColor(Color.yellow);
			g.setFont(new Font("Dialog", Font.BOLD, 14));
			g.drawString("Dealer's Hand: "+dealerHand.getBlackjackValue(), 20, 20);
			
			
			
			cardCount = userHand.getCardCount();
			for (int i = 0; i < cardCount; i++) {
				drawCard(userHand.getCard(i), g, xCor + i * (20 + cardWidth),
						yCor+140);
			}
			g.setColor(Color.yellow);
			g.setFont(new Font("Dialog", Font.BOLD, 14));
			g.drawString("Your Hand: "+userHand.getBlackjackValue(), 20, 160);
			
			g.setColor(Color.white);
			g.setFont(new Font("Dialog", Font.BOLD+Font.ITALIC, 14));
			g.drawString(""+message, 20, 300);
			

		}// end of compareLower()

		public void drawCard(Card card, Graphics g, int x, int y) {
			if (card == null) {
				g.setColor(Color.blue);
				g.fillRect(x, y, cardWidth, cardHeight);
				g.fillRect(x + 4, y + 4, cardWidth - 8, cardHeight - 8);
				g.setColor(Color.white);
				g.drawRect(x + 2, y + 2, cardWidth - 4, cardHeight - 4);
			} else {
				g.setColor(Color.gray);
				g.fillRect(x, y, cardWidth, cardHeight);

				g.setColor(Color.white);
				g.fillRect(x + 2, y + 2, cardWidth - 4, cardHeight - 4);

				g.setColor(Color.black);
				g.drawString(card.getValueAsString(), x + 10, y + 20);
				g.drawString(" of ", x + 10, y + 40);
				g.drawString(card.getSuitAsString(), x + 10, y + 60);
			}

		}

	}// end of class CardPanel

}
